package com.develop.web.in.request.json.field.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import lombok.extern.slf4j.Slf4j;
import org.javamoney.moneta.Money;
import org.springframework.boot.jackson.JsonComponent;

import javax.money.MonetaryAmount;
import java.io.IOException;
import java.math.BigDecimal;

@Slf4j
@JsonComponent
public class Json2MonetaryAmount extends StdDeserializer<MonetaryAmount> {
    protected Json2MonetaryAmount() {
        super(MonetaryAmount.class);
    }

    @Override
    public MonetaryAmount deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        //return Money.of(CurrencyUnit.of("CNY"), p.getDecimalValue());
        log.info(p.getValueAsString());
        log.info(p.getValueAsString().split(" ")[0]);
        log.info(p.getValueAsString().split(" ")[1]);
        return Money.of(new BigDecimal(p.getValueAsString().split(" ")[1]), p.getValueAsString().split(" ")[0]);
    }
}
